package lab3;

import java.util.Observable;

/**
 * This class registers students for courses.
 *  
 * @author A.J. Lattanze, CMU
 * @version 1.2, 2011-Feb-24
 */


/*
 * Modification Log **********************************************************
 * 
 * v1.3, Nigel Tingley, 2011-Mar-12 - Added new functionalities: 
 * 
 * v1.2, R. Champagne, 2011-Feb-24 - Various refactorings, conversion of
 * comments to javadoc format.
 * 
 * v1.1, R. Champagne, 2002-Jun-19 - Adapted for use at ETS.
 * 
 * v1.0, A.J. Lattanze, 12/29/99 - Original version.
 * ***************************************************************************
 */
public class RegisterStudent extends Communication
{
	public RegisterStudent(Integer registrationNumber, String componentName) {
		super(registrationNumber, componentName);
	}

	/**
	 * The update() method is an abstract method that is called whenever the
	 * notifyObservers() method is called by the Observable class. First we
	 * check to see if the NotificationNumber is equal to this thread's
	 * RegistrationNumber. If it is, then we execute.
	 * 
	 * @see ca.etsmtl.log430.lab3.Communication#update(java.util.Observable,
	 *      java.lang.Object)
	 */
	public void update(Observable thing, Object notificationNumber) {
		Menus menu = new Menus();
		Student myStudent = new Student();
		Course myCourse = new Course();

		if (registrationNumber.compareTo((Integer)notificationNumber) == 0) {
			addToReceiverList("ListStudentsComponent");
			addToReceiverList("ListCoursesComponent");

			// Display the students and prompt the user to pick a student

			signalReceivers("ListStudentsComponent");

			myStudent = menu.pickStudent(CommonData.theListOfStudents.listOfStudents);

			if (myStudent != null) {
				/*
				 * Display the courses that are available and ask the user to
				 * pick a course to register for
				 */
				signalReceivers("ListCoursesComponent");

				myCourse = menu.pickCourse(CommonData.theListOfCourses.getListOfCourses());

				if (myCourse != null)	{	
					/*
					 * If the selected course and student exist, then check if there 
					 * is a schedule conflict, THEN check if they've already 
					 * done the course before
					 */
					if (!myCourse.alreadyRegistered(myStudent)){
						if (myCourse.timeCheck(myStudent)){
							if (menu.repeatCheck(myStudent, myCourse)){
								myCourse.register(myStudent);
								myStudent.register(myCourse);
							} //if 
						} //if
					}  
				} else {
					System.out.println("\n\n *** Course not found ***");
				} 
			} else {
				System.out.println("\n\n *** Student not found ***");
			}
		}
	}
}